﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;
using Com;
using Core;
using Models;

namespace Controllers
{
    public class JxinxiController : AdminBaseController
    {
        jxinxiCore core = new jxinxiCore();
        public ActionResult Get(int id)
        {
            if (!HasQx("jxinxi"))
            {
                return Error("没有权限！");
            }
            return Success(core.GetOne(id));
        }

        public ActionResult GetPageList(pageinfo pageinfo)
        {
            if (!HasQx("jxinxi"))
            {
                return Error("没有权限！");
            }
            List<object> argsList = new List<object>();
            string where = string.Empty;
            //where += " and endtime>=now()";
            if (!string.IsNullOrEmpty(pageinfo.key))
            {
                where += " and title like @" + argsList.Count + "";
                argsList.Add($"%{pageinfo.key}%");
            }
            var list = core.GetPage(pageinfo.page, pageinfo.pagesize, where, pageinfo.order + " " + pageinfo.esc, argsList.ToArray());
            return Success(list);
        }
        [HttpPost]
        public ActionResult Del(int id)
        {
            if (!HasQx("jxinxi"))
            {
                return Error("没有权限！");
            }
            if (id > 0)
            {
                if (core.Del(id))
                {
                    Log(curmanage.username + "删除简单内容ID=" + id);
                    return Success("删除成功！");
                }

            }
            return Error("删除失败！");
        }
        [HttpPost]
        public ActionResult DelAll(List<jxinxiEntity> modelList)
        {
            if (!HasQx("jxinxi"))
            {
                return Error("没有权限！");
            }
            string ids = string.Empty;
            foreach (var jxinxiEntity in modelList)
            {
                core.Del(jxinxiEntity);
                ids += jxinxiEntity.ID + ",";
            }
            Log(curmanage.username + "删除简单内容ID in (" + ids + ")");
            return Success("删除成功！");
        }
        [HttpPost]
        public ActionResult Save(jxinxiEntity jxinxi)
        {
            if (!HasQx("jxinxi"))
            {
                return Error("没有权限！");
            }
            if (jxinxi.title== null)
            {
                return Error();
            }

            if (jxinxi.ID == 0)
            {
                jxinxi.sid = 1;//TODO 多站点调整
                jxinxi.mid = curmanage.ID;
            }
            var model= core.Save(jxinxi);
            Log(curmanage.username + "保存简单内容" + model.ToJson());
            return Success(model);
        }
    }
}
